package com.steptime.framework.spring.tx;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Transactional
@Service
public class EmpService extends JdbcDaoSupport{

	
	    public void insert(Emp emp) {
	            String sql= "insert emp(username,age,sex) values"
	                    +"('"+emp.getUsername()+"','"+emp.getAge()+"',"+emp.getSex()+"')" ;
	        this.getJdbcTemplate().update(sql);
	    }

	    public void update(Emp book) {
	        String sql = "update emp set username='"+book.getUsername()+"' where id='"+book.getId()+"' ";
	        this.getJdbcTemplate().update(sql);
	    }

	 
	    public Emp findBookById(String id) {
	        String sql =   "select id,username,age,sex from emp where id ='" + id + "'" ;
	       List<Emp> books =  this.getJdbcTemplate().query(sql,new RowMapper<Emp>() {
	            @Override
				public Emp mapRow(ResultSet rs, int rowNum) throws SQLException {
	                Emp bk  = new Emp();
	                bk.setId(rs.getInt("id"));
	                bk.setUsername(rs.getString("username"));
	                bk.setAge(rs.getString("age"));
	                bk.setSex(rs.getString("sex"));
	                return bk;
	            }
	        });
	        return books!=null?books.get(0):null ;
	    }

}
